package com.shapawan.tourism.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shapawan.tourism.common.Result;
import com.shapawan.tourism.entity.Location;
import com.shapawan.tourism.service.LocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 位置信息控制器
 */
@Api(tags = "位置信息管理")
@RestController
@RequestMapping("/api/locations")
public class LocationController {

    @Autowired
    private LocationService locationService;

    @ApiOperation("分页查询位置信息")
    @GetMapping("/page")
    public Result<IPage<Location>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer typeId,
            @RequestParam(required = false) String keyword) {

        Page<Location> page = new Page<>(current, size);
        IPage<Location> pageData = locationService.pageList(page, typeId, keyword);
        return Result.success(pageData);
    }

    @ApiOperation("获取推荐位置列表")
    @GetMapping("/recommend")
    public Result<List<Location>> getRecommendLocations() {
        return Result.success(locationService.getRecommendLocations());
    }

    @ApiOperation("获取指定类型的位置列表")
    @GetMapping("/byType/{typeId}")
    public Result<List<Location>> getLocationsByType(@PathVariable Integer typeId) {
        return Result.success(locationService.getLocationsByType(typeId));
    }

    @ApiOperation("获取位置详情")
    @GetMapping("/{id}")
    public Result<Location> getLocationDetail(@PathVariable Integer id) {
        locationService.increaseViewCount(id);
        return Result.success(locationService.getLocationDetail(id));
    }

    @ApiOperation("新增位置信息")
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'EDITOR')")
    public Result<Location> add(@Validated @RequestBody Location location) {
        locationService.save(location);
        return Result.success(location);
    }

    @ApiOperation("修改位置信息")
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'EDITOR')")
    public Result<String> update(@PathVariable Integer id, @Validated @RequestBody Location location) {
        location.setId(id);
        locationService.updateById(location);
        return Result.success("修改成功");
    }

    @ApiOperation("删除位置信息")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> delete(@PathVariable Integer id) {
        locationService.removeById(id);
        return Result.success("删除成功");
    }

    @ApiOperation("设置推荐状态")
    @PutMapping("/{id}/recommend/{status}")
    @PreAuthorize("hasAnyRole('ADMIN', 'EDITOR')")
    public Result<String> setRecommendStatus(@PathVariable Integer id, @PathVariable Integer status) {
        Location location = locationService.getById(id);
        if (location != null) {
            location.setIsRecommend(status);
            locationService.updateById(location);
            return Result.success("设置成功");
        }
        return Result.failed("位置不存在");
    }
} 